package com.efast.dmom.mgmt.service.excel.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.bean.DBColumn;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.excel.ExcelFieldAcceptBean;
import com.efast.dmom.mgmt.bean.excel.ExcelRuleDataBean;
import com.efast.dmom.mgmt.bean.excel.TwExcelRuleBean;
import com.efast.dmom.mgmt.dao.excel.ExcelUploadRuleConfigurationDao;
import com.efast.dmom.mgmt.service.excel.ExcelUploadRuleConfigurationService;

@Service
public class ExcelUploadRuleConfigurationImpl implements ExcelUploadRuleConfigurationService {

	@Autowired
	private ExcelUploadRuleConfigurationDao excelUploadRuleConfigurationDao;

	@Autowired
	@Qualifier("jdbcDao")
	private JdbcDao jdbcDao;

	@Override
	public List<String> getTableName(String name) {
		return excelUploadRuleConfigurationDao.getTableName(name);
	}

	@Override
	public List<ExcelFieldAcceptBean> getFiledNameAndcommentByTableName(String tableName) {
		List<ExcelFieldAcceptBean> excelRuleDataBeanList = new ArrayList<ExcelFieldAcceptBean>();
		if (tableName != null) {
			List<DBColumn> dbColum = jdbcDao.getColumns(tableName);
			if (dbColum != null && dbColum.size() > 0) {
				for (DBColumn data : dbColum) {
					ExcelFieldAcceptBean excelFieldAcceptBean = new ExcelFieldAcceptBean();
					excelFieldAcceptBean.setFieldName(data.getColumnName());
					excelFieldAcceptBean.setFieldComment(data.getColumnComment());
					excelRuleDataBeanList.add(excelFieldAcceptBean);
				}
			}
		}
		return excelRuleDataBeanList;
	}

	@Override
	public ResultBean createTableExcelRule(TwExcelRuleBean data) {
		ResultBean result = new ResultBean(true, "'");
		// 判断相应表的规则是否存在
		List<ExcelRuleDataBean> list = excelUploadRuleConfigurationDao.selectExcelRuleByTableName(data.getTableName());
		List<ExcelRuleDataBean> data1 = new ArrayList<ExcelRuleDataBean>();
		boolean flag = true;
		if (data.getRuleData() != null && data.getRuleData().size() > 0) {
			if (list != null && list.size() > 0) {
//				int id = 0;
//				for (ExcelRuleDataBean bean : list) {
//					if(bean.getRepeatField() != null) {
//						id =id+1;
//					}
//				}
//				for (ExcelRuleDataBean ruleData : data.getRuleData()) {
//					if(ruleData.getRepeatField() != null) {
//						id =id+1;
//					}
//				}
//				if(id>1) {
//					result.setSuccess(false);
//					result.setMessage("查重字段不唯一，请重新选择规则！");
//					return result;
//				}
				for (ExcelRuleDataBean ruleData : data.getRuleData()) {
					for (ExcelRuleDataBean bean : list) {
						if (ruleData.getFieldName().equals(bean.getFieldName())) {
							flag = false;
							break;
						}
					}
					if (!flag) {
						flag = true;
						result.setSuccess(false);
						result.setMessage("新增的字段规则表已存在,无法新增！");
					} else {
						ruleData.setCreateTime(new Date());
						ruleData.setCreateUser(SpringWebUtils.getRequestUser());
						data1.add(ruleData);
						flag = true;
					}
				}
				if (data1 != null && data1.size() > 0) {
					excelUploadRuleConfigurationDao.insertTwExcelRuleByData(data.getTableName(), data1);
					result.setSuccess(true);
					result.setMessage("新增的规则字段生成成功！");
				}
			} else {
//				int id = 0;
				for(ExcelRuleDataBean ruleData : data.getRuleData()) {
//					if(ruleData.getRepeatField() != null) {
//						id=id+1;
//					}
					ruleData.setCreateTime(new Date());
					ruleData.setCreateUser(SpringWebUtils.getRequestUser());
				}
//				if (id > 1) {
//					result.setSuccess(false);
//					result.setMessage("查重字段不唯一，请重新选择规则！");
//					return result;
//				}
				excelUploadRuleConfigurationDao.insertTwExcelRuleByData(data.getTableName(), data.getRuleData());
				result.setSuccess(true);
				result.setMessage("相应表的规则生成成功！");
			}
		}
		return result;
	}

	@Override
	public List<ExcelRuleDataBean> getInformationByRuleTable(String tableName) {
		return excelUploadRuleConfigurationDao.selectExcelRuleByTableName(tableName);
	}

	@Override
	public ResultBean updateTableExcelRule(TwExcelRuleBean data) {
		ResultBean result = new ResultBean(true, "'");
		List<ExcelRuleDataBean> list = excelUploadRuleConfigurationDao.selectExcelRuleByTableName(data.getTableName());
//		int id = 0;
//		if (list != null && list.size() > 0) {
//		for(ExcelRuleDataBean bean:list) {
//			if(bean.getRepeatField() != null) {
//				id = id+1;
//			}
//		 }
//		}
		for(ExcelRuleDataBean bean:data.getRuleData()) {
			if(bean.getRepeatField()!=null&&!bean.getRepeatField()) {
				bean.setRepeatField(null);
			}
//			if(bean.getRepeatField()!=null) {
//				id = id+1;
//			}
			bean.setLastUpdateTime(new Date());
			bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		}
//		if(id>1) {
//			result.setSuccess(false);
//			result.setMessage("查重字段不唯一！");
//			return result;
//		}
		excelUploadRuleConfigurationDao.updateTableExcelRule(data.getTableName(), data.getRuleData());
		result.setSuccess(true);
		result.setMessage("更新成功");
		return result;
	}

	@Override
	public void deleteTableExcelRule(TwExcelRuleBean data) {
		excelUploadRuleConfigurationDao.deleteTableExcelRule(data.getTableName(), data.getRuleData());
	}

	@Override
	public void saveTableExcelRule(TwExcelRuleBean ruleData) {

	}

}
